home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / MotifApp / ch5 / Engine.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  4.0 KB  |  162 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //////////////////////////////////////////////////////////////////////////////
  3. //         This example code is from the book:
  4. //
  5. //           Object-Oriented Programming with C++ and OSF/Motif
  6. //         by
  7. //           Douglas Young
  8. //           Prentice Hall, 1992
  9. //           ISBN 0-13-630252-1    
  10. //
  11. //         Copyright 1991 by Prentice Hall
  12. //         All Rights Reserved
  13. //
  14. //  Permission to use, copy, modify, and distribute this software for 
  15. //  any purpose except publication and without fee is hereby granted, provided 
  16. //  that the above copyright notice appear in all copies of the software.
  17. ///////////////////////////////////////////////////////////////////////////////
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20. // See Board.h for comments on changes to this file from the book version.
  21.  
  22.  
  23. /////////////////////////////////////////////////////////
  24. // Engine.C: The brains of the tic-tac-toe game
  25. /////////////////////////////////////////////////////////
  26. #include "TicTacToe.h"
  27. #include "Engine.h"
  28. #include "GameBoard.h"
  29. #include "MoveGenerator.h"
  30. #include "Message.h"
  31. #include "Board.h"
  32. #include <stdlib.h>  // Needed for exit()
  33.  
  34. Engine::Engine ( TicTacToe* game )
  35. {
  36.     _game      = game;
  37.     _gameOver  = FALSE;
  38.     _whoseMove = XX; // Start with X as the first move
  39.     _board = new Board(); // Create the Engine subcomponents
  40.     _moveGenerator = new MoveGenerator();
  41. }
  42.  
  43. Engine::~Engine()
  44. {
  45.     delete _board;
  46.     delete _moveGenerator;
  47. }
  48.  
  49. void Engine::reset()
  50. {
  51.     _whoseMove = XX;
  52.     _gameOver  = FALSE;
  53.     _board->clear();
  54.     _game->gameBoard()->clear();
  55.     _game->messageArea()->postMessage ( NEWGAMEMSG );
  56. }
  57.  
  58. void Engine::recordMove ( int position )
  59. {
  60.     if ( _gameOver ) // Don't accept moves if the game is over
  61.     {
  62.     _game->messageArea()->postAlert( GAMEISOVERMSG );
  63.     return;
  64.     }
  65.     
  66.     // Record the move. If it is valid, display it on the board
  67.     // Otherwise ask the user to pick again.
  68.     
  69.     if ( _board->recordMove ( position, _whoseMove ) == validMove)
  70.     {
  71.     if(_whoseMove == XX)
  72.         _game->gameBoard()->markX ( position );
  73.     else
  74.         _game->gameBoard()->markO ( position );
  75.     }
  76.     else
  77.     {
  78.     _game->messageArea()->postAlert ( ILLEGALMOVEMSG );
  79.     return;
  80.     }
  81.     
  82.     // See if this move wins the game for the user
  83.     
  84.     checkForWin();
  85.     
  86.     if ( _gameOver )
  87.     return;
  88.     
  89.     // If this is the game's move, change to X's move and ask the 
  90.     // user to choose a square. If it is the user's move, change to
  91.     // our move and pick a move. Call this function
  92.     // recursively to record the game's choice.
  93.     
  94.     if ( _whoseMove == OO )
  95.     {    
  96.     _whoseMove = XX;
  97.     _game->messageArea()->postMessage ( USERSMOVEMSG );
  98.     }
  99.     else
  100.     {
  101.     _whoseMove = OO;
  102.     recordMove ( _moveGenerator->getNextMove ( _board ) );
  103.     }
  104. }
  105.  
  106. void Engine::checkForWin()
  107. {
  108.     int      i, *winningSquares;
  109.     markType winner;
  110.     
  111.     // If no one has won yet, just keep playing
  112.     
  113.     if    ( ( winner = _board->whoHasWon() ) == NOBODYYET )
  114.     {
  115.     return;
  116.     }
  117.     else if ( winner == TIE)
  118.     {
  119.     // If it's a tie, end the game and notify the user
  120.     
  121.     _gameOver = TRUE;    
  122.     
  123.     for ( i = 0 ; i < 9; i++ )
  124.         _game->gameBoard()->deemphasizeSquare ( i );
  125.     
  126.     _game->messageArea()->postAlert( TIEGAMEMSG );
  127.     }
  128.     else // Someone won 
  129.     {
  130.     _gameOver = TRUE;    
  131.     
  132.     // Get the mask for the wining pattern 
  133.     
  134.     winningSquares = _board->winningSquares();
  135.     
  136.     // Deactivate each square to prevent input. Highlight
  137.     // the winning squares, and fade others into the background.
  138.     
  139.     for ( i = 0 ; i < 9; i++ )
  140.     {
  141.         _game->gameBoard()->deactivateSquare ( i );
  142.         
  143.         if ( winningSquares[i] )        
  144.         _game->gameBoard()->highlightSquare ( i );
  145.         else
  146.         _game->gameBoard()->deemphasizeSquare ( i );
  147.     }
  148.     
  149.     // Finally, alert the user that someone has won
  150.     
  151.     if ( winner  == XX )
  152.         _game->messageArea()->postAlert ( XWINSMSG );
  153.     else
  154.         _game->messageArea()->postAlert ( OWINSMSG );    
  155.     }
  156. }
  157.  
  158. void Engine::quit()
  159. {
  160.     exit ( 0 );
  161. }
  162.